package com.yuanfeng.order.utils.sql;


import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class BusinessManageSql {

    /**
     * 查询商家订单
     *
     * @param para
     * @return
     */
    public String queryOrderBase(Map<String, Object> para) {
        String sql = new SQL() {
            {	/*,oob.virtual_goods,oob.period_of_validity,oob.past_due,oob.redeem_code,oob.redeem_code_url,oob.is_cancel,oob.cancel_date */
                SELECT("oob.order_id,oob.order_create_time,oob.buyer_user_name,oob.order_payment_amount," +
                        "oob.order_status,oob.order_shipping_fee,oob.pay_code,oob.order_shipping_code,oob.order_receiver_name," +
                        "oob.order_receiver_address,oob.order_receiver_contact,oob.order_refund_status,oob.order_return_status," +
                        "oe.express_pinyin,oob.giveaway_info,oob.drop_shipping,oob.plus_order as plusOrder");
                FROM("order_base oob LEFT JOIN order_express oe on oe.express_id = oob.order_shipping_express_id ");
                if (para.get("allOrderStatus") == "' '") {
                    WHERE( "1 = 1" );
                }
                if (para.get("plusOrder") != null && para.get("plusOrder") != "") {
                    WHERE("oob.plus_order = " + para.get("plusOrder"));
                }
                if (para.get("otherOrderStatus") != "" && para.get("otherOrderStatus") != null) {
                    WHERE("oob.order_status = " + para.get("otherOrderStatus"));
                }
                if (para.get("orderReturnStatus") != "" && para.get("orderReturnStatus") != null) {
                    WHERE("oob.order_return_status = " + para.get("orderReturnStatus"));
                }
                if (para.get("orderRefundStatus") != "" && para.get("orderRefundStatus") != null) {
                    WHERE("oob.order_refund_status = " + para.get("orderRefundStatus"));
                }
                if (para.get("userName") != "" && para.get("userName") != null) {
                    WHERE("oob.buyer_user_name like CONCAT('%'," + para.get("userName")+",'%')");
                }
                if (para.get("orderNum") != "" && para.get("orderNum") != null) {
                    WHERE("oob.order_id  like CONCAT('%'," + para.get("orderNum")+",'%')");
                }
                if (para.get("orderStartTime") != "" && para.get("orderStartTime") != null) {
                    WHERE("oob.order_create_time >= " + para.get("orderStartTime"));
                }
                if (para.get("orderEndTime") != "" && para.get("orderEndTime") != null) {
                    WHERE("oob.order_create_time <= " + para.get("orderEndTime"));
                }
                if (para.get("deliverShopName") != "" && para.get("deliverShopName") != null && !para.get("deliverShopName").equals("null")) {
                    WHERE("oob.deliver_shop_name LIKE " + "'%" + para.get("deliverShopName") + "%'");
                }
                if (para.get("orderType") != "" && para.get("orderType") != null && para.get("orderType").equals(2)) {
                    WHERE("oob.order_type = " + para.get("orderType") + " and oob.order_is_shop_take != 1");
                }else{
                    WHERE("oob.order_type != " + 2 + " and oob.order_is_shop_take != 1");
                }
                if (para.get("dropShipping") != "" && para.get("dropShipping") != null) {
                    WHERE("oob.drop_shipping = " + para.get("dropShipping"));
                }
                if(para.get("dropShipping") != "" && para.get("dropShipping") != null && para.get("dropShipping").equals(1)
                        && para.get("orderSource") != "" && para.get("orderSource") != null && para.get("orderSource").equals(1)){
                    WHERE("oob.deliver_shop_id = " + para.get("shopId"));
                }else {
                    WHERE("oob.shop_id = " + para.get("shopId"));
                }
                WHERE("oob.virtual_goods = 1");
            }
        }.toString();
        StringBuilder sb = new StringBuilder(sql);
        sb.append(" and oob.order_shop_hidden = 0 order by oob.order_create_time desc limit #{cp},#{pageSize}");
        return sb.toString();
    }

    /**
     * 按需获取总数
     *
     * @param para
     * @return
     */
    public String getOrderConut(Map<String, Object> para) {
        String sql = new SQL() {
            {
                SELECT("count(*)");
                FROM("order_base oob");
                if (para.get("allOrderStatus") == "' '") {
                    WHERE("oob.order_status != " + para.get("allOrderStatus"));
                }
                if (para.get("plusOrder") != null && para.get("plusOrder") != "") {
                    WHERE("oob.plus_order = " + para.get("plusOrder"));
                }
                if (para.get("otherOrderStatus") != "" && para.get("otherOrderStatus") != null) {
                    WHERE("oob.order_status = " + para.get("otherOrderStatus"));
                }
                if (para.get("orderReturnStatus") != "" && para.get("orderReturnStatus") != null) {
                    WHERE("oob.order_return_status = " + para.get("orderReturnStatus"));
                }
                if (para.get("orderRefundStatus") != "" && para.get("orderRefundStatus") != null) {
                    WHERE("oob.order_refund_status = " + para.get("orderRefundStatus"));
                }
                if (para.get("userName") != "" && para.get("userName") != null) {
                    WHERE("oob.buyer_user_name  like CONCAT('%',"+para.get("userName")+",'%')");
                }
                if (para.get("orderNum") != "" && para.get("orderNum") != null) {
                    WHERE("oob.order_id  like CONCAT('%'," + para.get("orderNum")+",'%')");
                }
                if (para.get("orderStartTime") != "" && para.get("orderStartTime") != null) {
                    WHERE("oob.order_create_time >= " + para.get("orderStartTime"));
                }
                if (para.get("orderEndTime") != "" && para.get("orderEndTime") != null) {
                    WHERE("oob.order_create_time <= " + para.get("orderEndTime"));
                }
                if (para.get("deliverShopName") != "" && para.get("deliverShopName") != null && !para.get("deliverShopName").equals("null")) {
                    WHERE("oob.deliver_shop_name LIKE ('%"+ para.get("deliverShopName")+"%')");
                }
                if (para.get("orderType") != "" && para.get("orderType") != null && para.get("orderType").equals(2)) {
                    WHERE("oob.order_type = " + para.get("orderType") + " and oob.order_is_shop_take != 1");
                }else{
                    WHERE("oob.order_type != " + 2 + " and oob.order_is_shop_take != 1");
                }
                if (para.get("dropShipping") != "" && para.get("dropShipping") != null) {
                    WHERE("oob.drop_shipping = " + para.get("dropShipping"));
                }
                if(para.get("dropShipping") != "" && para.get("dropShipping") != null && para.get("dropShipping").equals(1)
                        && para.get("orderSource") != "" && para.get("orderSource") != null && para.get("orderSource").equals(1)){
                    WHERE("oob.deliver_shop_id = " + para.get("shopId"));
                }else {
                    WHERE("oob.shop_id = " + para.get("shopId"));
                }
                WHERE("oob.virtual_goods = 1");
            }
        }.toString();
        StringBuilder sb = new StringBuilder(sql);
        sb.append("and oob.order_shop_hidden = 0");
        return sb.toString();
    }



}
